package com.hn.kiss.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.kiss.config.common.CommonPage;
import com.hn.kiss.config.common.CommonResult;
import com.hn.kiss.config.compoent.BaseController;
import com.hn.kiss.config.query.QueryGenerator;
import com.hn.kiss.modules.sys.dto.MenuTreeDTO;
import com.hn.kiss.modules.sys.entity.Menu;
import com.hn.kiss.modules.sys.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(tags = "SysMenuController", description = "菜单表管理")
@RestController
@RequestMapping("/sys/menu")
@Validated
public class MenuController extends BaseController {

    @Autowired
    IMenuService menuService;

    @ApiOperation("菜单表分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<Menu> pageInfo(@NotNull Integer pageNo,
                                     @NotNull Integer pageSize,
                                     Menu menu,
                                     HttpServletRequest request){
        Page<Menu> pageInfo = new Page<>(pageNo, pageSize);
        //构造查询条件
        QueryWrapper<Menu> qw = QueryGenerator.initQueryWrapper(menu, request.getParameterMap());
        Page<Menu> page1 = menuService.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("菜单树形列表")
    @ApiImplicitParam(name = "category", value = "菜单类别", required = true, dataType = "String", paramType = "query")
    @GetMapping("/tree")
    public CommonResult<List<MenuTreeDTO>> tree(@NotBlank String category){
        List<MenuTreeDTO> tree = menuService.tree(category);
        return CommonResult.success(tree);
    }

    @ApiOperation("通过主键获取菜单表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{id}")
    public CommonResult<Menu> getByPk(@NotBlank@PathVariable("id") String id){
        Menu menu = menuService.getByPk(id);
        return CommonResult.success(menu);
    }

    @ApiOperation("新增菜单表")
    @PostMapping
    public CommonResult add(@Validated Menu menu, BindingResult bindingResult){
    boolean b = menuService.save(menu);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改菜单表")
    @PutMapping
    public CommonResult updateByPk(@Validated Menu menu, BindingResult bindingResult){
        boolean b = menuService.updateByPk(menu);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除菜单表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public CommonResult removeByPk(@NotBlank@PathVariable("id") String id){
        boolean b = menuService.removeByPk(id);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }



}
